local wanrong = fk.CreateSkill{
  name = "lvshi__wanrong",
  tags = { "Phase" }
}

Fk:loadTranslationTable{
  ["lvshi__wanrong"] = "挽戎",
  [":lvshi__wanrong"] = "阶段技，或己方角色的濒死结算结束后，你可以令一名其他角色视为对你在内的两名角色使用【里应外合】或【杀】，然后你移动场上一张牌。",
  ["#lvshi__wanrong-choose"] = "你可以令一名其他角色视为对你在内的两名角色使用【里应外合】或【杀】",
  ["#lvshi__wanrong-use"] = "请使用【里应外合】或【杀】选择一名除%src的角色，此牌目标为该角色和%src",
  ["#lvshi__wanrong-move"] = "你可以移动场上一张牌",

  ["$lvshi__wanrong1"] = "统帅三军，怎能不识变数？",
  ["$lvshi__wanrong2"] = "行军打仗，须得随机应变。",
}
local U = require "packages/utility/utility"
local ls = require "packages/lvshi/utility"
wanrong:addEffect(fk.AfterDying, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wanrong.name) and ls.isFriend(player, target)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local result = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#lvshi__wanrong-choose",
      skill_name = wanrong.name,
    })
    if #result > 0 then
      room:sortByAction(result)
      event:setCostData(self, {tos = result})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cardDic = {{"lvshi__slash", Card.NoSuit, 0}, {"lvshi__collaborate", Card.NoSuit, 0}}
    local cards = U.prepareDeriveCards(room, cardDic, "wanrong_cards")
    local use = room:askToUseRealCard(to, {
      pattern = cards,
      skill_name = wanrong.name,
      prompt = "#lvshi__wanrong-use:"..player.id,
      extra_data = { expand_pile = cards,
      bypass_distances = true,
      exclusive_targets = table.map(room:getOtherPlayers(player), Util.IdMapper)},
      skip = true,
    })
    if use then
      --room:useCard(use)
      local tos = use:getAllTargets()
      table.insertIfNeed(tos, player)
      room:useVirtualCard(use.card.name, nil, to, tos, wanrong.name, true)
      local targets = player.room:askToChooseToMoveCardInBoard(player, {
        skill_name = wanrong.name,
        cancelable = true,
        prompt = "#lvshi__wanrong-move",
      })
      if #targets == 2 then
        room:askToMoveCardInBoard(player, {
          target_one = targets[1],
          target_two = targets[2],
          skill_name = wanrong.name,
        })
      end
    end
  end,
  --[[on_cost = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      local use = room:askToUseVirtualCard(move.from, {
        name = "snatch",
        skill_name = wanrong.name,
        prompt = "#lzsj__wanrong-use",
        cancelable = true,
        skip = true,
      })
      if use then
        event:setCostData(self, {use = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local use = event:getCostData(self).use
    player.room:useCard(use)
  end,]]
})

wanrong:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  --max_phase_use_time = 1,
  prompt = "#lvshi__wanrong-choose",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
  return to_select ~= player
  end,
  can_use = function(self, player)
    return player:getMark("ls__wanrong-phase") == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    room:addPlayerMark(player, "ls__wanrong-phase")
    local cardDic = {{"lvshi__slash", Card.NoSuit, 0}, {"lvshi__collaborate", Card.NoSuit, 0}}
    local cards = U.prepareDeriveCards(room, cardDic, "wanrong_cards")
    local use = room:askToUseRealCard(to, {
      pattern = cards,
      skill_name = wanrong.name,
      prompt = "#lvshi__wanrong-use:"..player.id,
      extra_data = { expand_pile = cards,
      bypass_distances = true,
      exclusive_targets = table.map(room:getOtherPlayers(player), Util.IdMapper)},
      skip = true,
    })
    if use then
      --room:useCard(use)
      local tos = use.tos
      table.insertIfNeed(tos, player)
      room:useVirtualCard(use.card.name, nil, to, tos, wanrong.name, true)
      local targets = player.room:askToChooseToMoveCardInBoard(player, {
        skill_name = wanrong.name,
        cancelable = true,
        prompt = "#lvshi__wanrong-move",
      })
      if #targets == 2 then
        room:askToMoveCardInBoard(player, {
          target_one = targets[1],
          target_two = targets[2],
          skill_name = wanrong.name,
        })
      end
    end
  end,
})
return wanrong